//
//  XGL_VideoItemCollectionViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/17.
//

import UIKit

class XGL_VideoItemCollectionViewCell: UICollectionViewCell {
    typealias LoadImageComplete = (_ image: UIImage)->Void
    var coverImageView: UIImageView!
    var userHeaderView: UIImageView!
    var userNameText: UILabel!
    var publishDateText: UILabel!
    var thumbItem: UIButton!
    var titleText: UILabel!
    var labelView: VideoMarkView!
    var playItem: UIImageView!
    
    var complete: LoadImageComplete?
    var itemData: PublishData = PublishData(){
        didSet{
            var imageUrl = ""
            if self.itemData.data4 == "2"{
                if let url = self.itemData.data7 {
                    imageUrl = url
                }
                self.playItem.isHidden = false
            }else{
                self.playItem.isHidden = true
                if let url = self.itemData.data3?.components(separatedBy: ",").first{
                    imageUrl = url
                }
            }
            let cache = XGL_WaterfallImageCacheManager.shared.infor
            if let imageData = cache.imageDic[imageUrl],let image = UIImage(data: imageData) {
                self.coverImageView.image = image
            }else{
                self.coverImageView.loadImageWithUrl(imageUrl) { [weak self] image in
                    guard let `self` = self else { return }
                    let cache = XGL_WaterfallImageCacheManager.shared.infor
                    cache.imageDic[imageUrl] = image.jpegData(compressionQuality: 0.9)
                    XGL_WaterfallImageCacheManager.shared.saveInfor(inforData: cache)
                    if let block = self.complete {
                        block(image)
                    }
                }
            }
            if let infor = self.itemData.auditUserExt {
                self.userHeaderView.loadImageWithUrl(infor.data2)
                self.userNameText.text = infor.data1
            }
            self.publishDateText.text = convertDate(time: self.itemData.createTime)
            self.titleText.text = itemData.data2
            self.thumbItem.isSelected = self.itemData.listActionType.contains(2)
            self.labelView.labels = self.itemData.data6.components(separatedBy: ",")
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension XGL_VideoItemCollectionViewCell{
    private func createXglView(){
        self.coverImageView = UIImageView().then({ make in
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
        })
        self.userHeaderView = UIImageView().then({ make in
            make.layerCornerRadius = 15.0
            make.clipsToBounds = true
        })
        self.userNameText = UILabel().then({ make in
            make.textColor = .white
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        })
        self.publishDateText = UILabel().then({ make in
            make.textColor = .white
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        })
        self.thumbItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_thumb_normal"), for: .normal)
            make.setImage(imageWithName("icon_thumb_selected"), for: .selected)
        })
        self.titleText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.numberOfLines = 2
            make.lineBreakMode = .byTruncatingTail
        })
        self.labelView = VideoMarkView()
        self.playItem = UIImageView().then({ make in
            make.image = imageWithName("icon_video_play")
            make.isHidden = true
        })
        self.contentView.addSubviews([self.coverImageView,userHeaderView,userNameText,publishDateText,thumbItem,titleText,labelView,playItem])
        self.coverImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        userHeaderView.snp.makeConstraints { make in
            make.left.top.equalTo(12.0)
            make.width.height.equalTo(30.0)
        }
        self.userNameText.snp.makeConstraints { make in
            make.left.equalTo(userHeaderView.snp.right).offset(5.0)
            make.top.equalTo(userHeaderView)
        }
        self.publishDateText.snp.makeConstraints { make in
            make.left.equalTo(userNameText)
            make.bottom.equalTo(userHeaderView)
        }
        thumbItem.snp.makeConstraints { make in
            make.right.equalTo(-10.0)
            make.top.equalTo(10.0)
            make.width.height.equalTo(24.0)
        }
        self.titleText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(12.0)
            make.bottom.equalTo(self.labelView.snp.top).offset(-4.0)
        }
        self.labelView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(4.0)
            make.bottom.equalTo(-8.0)
        }
        playItem.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(42.0)
        }
    }
}
